package kisti.lod.classification;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.tdb.TDBFactory;

public class PredFrequency {
	
	//TODO: general model has to be created
	protected static String generalModel = FileFactory.getTdbFolder();
	
	public static void main (String args[]) {
		StringBuffer frequencies = new StringBuffer();
		frequencies.append("Predicate;Count");
		
		Dataset ds = TDBFactory.createDataset(generalModel);
	    Model model = ds.getDefaultModel();
	    
	    String query = "SELECT ?p (COUNT(?p) as ?pCount)" +
	    		"WHERE " +
	    		"  {" +
	    		"    ?s ?p ?o ." +
	    		" 	 FILTER (!isBlank(?s)) . " +
	    		"  }" +
	    		"GROUP BY ?p";
//	    		"ORDER BY DESC(?pCount)";
		Query sparqlQuery = QueryFactory.create(query);
		QueryExecution quexec = QueryExecutionFactory.create(sparqlQuery, model);
		
		ResultSet results = quexec.execSelect();
		while(results.hasNext()) {
			QuerySolution soln = results.nextSolution();
			String predicate = soln.getResource("p").toString();
			Integer count = soln.getLiteral("pCount").getInt();
			frequencies.append(predicate + ";" + count + "\n");
		}
		System.out.println(frequencies.toString());
	}
}